import api from "../../utils/api";
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    deptName: '',
    date: '', //创建时间
    UnitNo: '',
    UnitName: '',
    type: '',
    // IntercourseUnit_id: '',
    tax: '',
    address:'',
    openingBank:'',
    columns_CustomerType: [], //往来单位列表
    showType: false,
    address:'',
    openingBank:''
  },
  onUnitNoChange(e) {
    this.setData({
      UnitNo: e.detail
    })
  },
  onUnitNameChange(e) {
    this.setData({
      UnitName: e.detail
    })
  },
  //地址+电话
  onaddressChange(e) {
    console.log(e)
    this.setData({
      address: e.detail
    })
    console.log(this.data.address, '地址+电话')
  },
  //开户行+银行账号
  onopeningBankChange(e) {
    this.setData({
      openingBank: e.detail
    })
    console.log(this.data.openingBank, '开户行+银行账号')
  },
  // 选择往来单位 跳转到页面
  iconclick(e) {
    //类别列表初始渲染
    let that = this;
    api.getCustomerTypeDict({
      success: function (res) {
        console.log(res.data.data, "getCustomerTypeDict")
        if (res.data.code == 200) {
          that.data.columns_CustomerType = [];
          if (res.data.data.length > 0) {
            res.data.data.forEach((item) => {
              let obj = {
                text: item.dictValue,
                dictType: item.dictType,
                dictKey: item.dictKey
              }
              that.data.columns_CustomerType.push(obj)
              // console.log(that.data.columns, 'oooo')
            });
            that.setData({
              columns_CustomerType: that.data.columns_CustomerType,
            })
            that.setData({
              showType: true
            })
          } else {
            that.setData({
              showType: false
            })
          }
          // var timestamp=new Date().getTime();
          // console.log(timestamp)
          // this.setData({
          //     timestamp:formatDate(timestamp)
          // })

        }
      }
    })
  },
  onCancelType() {
    this.setData({
      showType: false
    });
  },
  onConfirmType(e) {
    const {
      value,
    } = e.detail;
    console.log(value, "val")
    let dictType = value.dictType
    let text = value.text;
    let dictKey = value.dictKey
    this.data.text = text;
    this.data.dictKey = dictKey;
    this.setData({
      type: value.text,
      showType: false,
    })
  },
  ontaxChange(e) {
    this.setData({
      tax: e.detail
    })
  },
  //点击保存,新键往来单位
  save() {
    console.log(this.data.address ,this.data.openingBank)
    if (this.data.UnitNo == '') {
      wx.showToast({
        title: '请先输入单位编码',
        icon: 'none'
      })
    } else if (this.data.UnitName == '') {
      wx.showToast({
        title: '请先输入单位名称',
        icon: 'none'
      })
    } else if (this.data.tax == '') {
      wx.showToast({
        title: '请先输入税号',
        icon: 'none'
      })
    } else if(!this.data.dictKey){
      wx.showToast({
        title: '请选择类别',
        icon: 'none'
      })
    }else if (this.data.address == '') {
      wx.showToast({
        title: '请先输入地址+电话',
        icon: 'none'
      })
    } else if (this.data.openingBank == '') {
      wx.showToast({
        title: '请先输入开户行+银行账号',
        icon: 'none'
      })
    } else {
      let that = this;
      api.addSysCustomerOne({
        method: 'POST',
        data: {
          name: this.data.UnitName,
          code: this.data.UnitNo,
          itin: this.data.tax,
          type: this.data.dictKey,
          address: this.data.address,
          openingBank: this.data.openingBank,
        },
        success: function (res) {
          console.log(res.data.data, "addSysCustomerOne")
          if (res.data.code == 200) {
            wx.showToast({
              title: '保存成功',
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 0,
              })
            }, 100);
          } else {
            wx.showModal({
              content: res.data.message,
              showCancel: false,
              confirmColor: '#0041C4'
            })
          }
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      IntercourseUnit_id: options.id,
      IntercourseUnit: options.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.coId = wx.getStorageSync('userInfo').coId
    this.setData({
      userName: wx.getStorageSync('userInfo').unitName,
      deptName: wx.getStorageSync('userInfo').deptName,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})